//
//  MYReplenishmentViewController.m
//  MiYuan_iOS
//
//  Created by zhangyongbing on 2018/4/18.
//  Copyright © 2018年 zhangyongbing. All rights reserved.
//

#import "MYReplenishmentViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "MYIndustrialControlViewController.h"

@interface MYReplenishmentViewController ()<AVCaptureMetadataOutputObjectsDelegate>
{
    int num;
    BOOL upOrdown;
    NSTimer * timer;
}
@property (strong,nonatomic)AVCaptureDevice * device;
@property (strong,nonatomic)AVCaptureDeviceInput * input;
@property (strong,nonatomic)AVCaptureMetadataOutput * output;
@property (strong,nonatomic)AVCaptureSession * session;
@property (strong,nonatomic)AVCaptureVideoPreviewLayer * preview;
@property (nonatomic, retain) UIImageView * line;
@property (nonatomic,weak) IBOutlet   UIView          *viewHead;

@end

@implementation MYReplenishmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSString *version= [UIDevice currentDevice].systemVersion;
    
    if(version.doubleValue >=11.0) {
        
    }else{
        // 针对 11.0 以下的iOS系统进行处理
        for (NSLayoutConstraint *constraint in self.viewHead.superview.constraints) {
            if (constraint.firstItem == self.viewHead && constraint.firstAttribute == NSLayoutAttributeTop) {
                constraint.constant = 20;
            }
        }
        
    }
     [self setupCamera];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_session startRunning];
   
}

/**调用系统二维码扫描*/
- (void)setupCamera
{
    // Device
    _device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    // Input
    _input = [AVCaptureDeviceInput deviceInputWithDevice:self.device error:nil];
    
    // Output
    _output = [[AVCaptureMetadataOutput alloc]init];
    [_output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    // Session
    _session = [[AVCaptureSession alloc]init];
    [_session setSessionPreset:AVCaptureSessionPresetHigh];
    if ([_session canAddInput:self.input])
    {
        [_session addInput:self.input];
    }
    
    if ([_session canAddOutput:self.output])
    {
        [_session addOutput:self.output];
    }
    
    // 条码类型 AVMetadataObjectTypeQRCode
    _output.metadataObjectTypes =@[AVMetadataObjectTypeQRCode,
                                   AVMetadataObjectTypeAztecCode,
                                   AVMetadataObjectTypeQRCode,
                                   AVMetadataObjectTypePDF417Code,
                                   AVMetadataObjectTypeEAN13Code,
                                   AVMetadataObjectTypeEAN8Code,
                                   AVMetadataObjectTypeCode128Code];
    
    // Preview
    _preview =[AVCaptureVideoPreviewLayer layerWithSession:self.session];
    _preview.videoGravity = AVLayerVideoGravityResizeAspectFill;
    _preview.frame =CGRectMake(0,62,WINDOWWIDTH,WINDOWHiGHT);
    [self.view.layer insertSublayer:self.preview atIndex:0];
    
    
    
    // Start
    [_session startRunning];
}

#pragma mark AVCaptureMetadataOutputObjectsDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection
{
    
    NSString *stringValue;
    
    if ([metadataObjects count] >0)
    {
        AVMetadataMachineReadableCodeObject * metadataObject = [metadataObjects objectAtIndex:0];
        stringValue = metadataObject.stringValue;
    }
    
    [_session stopRunning];
    
//    if (self.mdelegate && [self.mdelegate respondsToSelector:@selector(onScanQRViewBack:)]) {
//        [self.mdelegate onScanQRViewBack:stringValue];
//    }
    
    NSString *controlNumber = @"";
    NSString *date = @"";
    NSArray *array1 = [stringValue componentsSeparatedByString:@"?"];
    if ([array1 count] > 0) {
        NSString *string1 = [array1 objectAtIndex:array1.count-1];
        NSArray *array2 = [string1 componentsSeparatedByString:@"&"];
        if ([array2 count] > 0) {
            NSString *string = [array2 objectAtIndex:0];
            NSArray *array = [string componentsSeparatedByString:@"="];
            if ([array count] > 0) {
                controlNumber = [array objectAtIndex:array.count - 1];
            }
        }
        
        if ([array2 count] > 1) {
            NSString *string = [array2 objectAtIndex:1];
            NSArray *array = [string componentsSeparatedByString:@"="];
            if ([array count] > 0) {
                date = [array objectAtIndex:array.count - 1];
            }
        }
    }
    
    MYIndustrialControlViewController *ctrl = [[MYIndustrialControlViewController alloc] initWithControlNumber:controlNumber];
    [[PublicDefine shareInstance].navigation pushViewController:ctrl animated:YES];
}


@end
